﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using yq.ControllerBase;
using System.Web.Mvc;
using yq.WebModel;

namespace yq.Controller.Sys
{
    /// <summary>
    /// 用户管理
    /// </summary>
    public class UserController : BaseController
    {
        /// <summary>
        /// 查询用户列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            yq.Search.User.AccountSeek search = new Search.User.AccountSeek();

            return View(search);
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        [ErrorFilter("获取列表失败", ErrorFilterType.Text)]
        public ActionResult List(Search.User.AccountSeek search)
        {
            TryUpdateModel(search);
            TryUpdateModel(search.Pager);

            var list = yq.GetService.Create<yq.IService.IUser>().SelectAccountPagerList(search);

            ListPage<yq.Model.Logic.UserAccount> ListPage = new ListPage<Model.Logic.UserAccount>();
            ListPage.PageList = list;

            ListPage.AddRoleLinkButton("查看", "ViewUser", "User");
            ListPage.AddRoleLinkButton("编辑", "EidtUser", "User");

            return PartialView(ListPage);
        }

        /// <summary>
        /// 查看用户
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("userId")]
        [ErrorFilter("没有找到用户", ErrorFilterType.Text)]
        public ActionResult ViewUser()
        {
            yq.WebModel.Sys.User info = new WebModel.Sys.User();
            info.Account = this.GetModel<yq.Model.Logic.UserAccount>();
            info.Login = yq.GetService.Create<yq.IService.IUser>().SelectUserLoginList(info.Account.UserId);
            info.Info = yq.Model.Logic.UserInfo.Load(info.Account.UserId);

            return PartialView(info);
        }

        /// <summary>
        /// 编辑用户
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("userId")]
        public ActionResult EidtUser()
        {
            yq.WebModel.Sys.User info = new WebModel.Sys.User();
            info.Account = this.GetModel<yq.Model.Logic.UserAccount>();
            info.Login = yq.GetService.Create<yq.IService.IUser>().SelectUserLoginList(info.Account.UserId);
            info.Info = yq.Model.Logic.UserInfo.Load(info.Account.UserId);

            return View(info);
        }

        /// <summary>
        /// 编辑用户
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("Account.UserId")]
        [ErrorFilter("编辑用户失败", ErrorFilterType.ReturnValue)]
        public ActionResult DoEidtUser()
        {
            yq.WebModel.Sys.User info = new WebModel.Sys.User();
            info.Account = this.GetModel<yq.Model.Logic.UserAccount>();
            info.Info = yq.Model.Logic.UserInfo.Load(info.Account.UserId);
            TryUpdateModel(info);

            var rv = yq.GetService.Create<yq.IService.IUser>().EidtAccountAndInfo(info.Account, info.Info);

            return Json(rv);
        }

        /// <summary>
        /// 添加登录账号
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("UserId")]
        public ActionResult AddLogin()
        {
            var model = this.GetModel<yq.Model.Logic.UserAccount>();

            yq.Model.Logic.UserLogin log = new Model.Logic.UserLogin()
            {
                UserId = model.UserId
            };

            return PartialView(log);
        }

        /// <summary>
        /// 添加登录账号
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("UserId")]
        [ErrorFilter("添加账号失败", ErrorFilterType.ReturnValue)]
        public ActionResult DoAddLogin()
        {
            var log = new yq.Model.Logic.UserLogin();
            TryUpdateModel(log);
            var rv = yq.GetService.Create<yq.IService.IUser>().AddLogin(log);

            return Json(rv);
        }



        /// <summary>
        /// 编辑登录账号
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("logId")]
        [ErrorFilter("没有找到用户", ErrorFilterType.Text)]
        public ActionResult EidtLogin()
        {
            var model = this.GetModel<yq.Model.Logic.UserLogin>();

            return PartialView(model);
        }


        /// <summary>
        /// 重置密码
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("logId")]
        [ErrorFilter("重置失败", ErrorFilterType.ReturnValue)]
        public ActionResult ReatLogin()
        {
            var model = this.GetModel<yq.Model.Logic.UserLogin>();
            var rv = yq.GetService.Create<yq.IService.IUser>().ReatLogin(model.Rowid);

            return Json(rv);
        }

        /// <summary>
        /// 删除账号
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("logId")]
        [ErrorFilter("删除失败", ErrorFilterType.ReturnValue)]
        public ActionResult DelLogin()
        {
            var model = this.GetModel<yq.Model.Logic.UserLogin>();
            var rv = yq.GetService.Create<yq.IService.IUser>().DelLogin(model.Rowid);

            return Json(rv);
        }



        /// <summary>
        /// 添加用户
        /// </summary>
        /// <returns></returns>
        public ActionResult AddUser()
        {
            yq.WebModel.Sys.AddUser addUser = new WebModel.Sys.AddUser() 
            { 
             Account=new Model.Logic.UserAccount (),
              Info=new Model.Logic.UserInfo(),
               Login=new Model.Logic.UserLogin ()
            };

            return View(addUser);
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <returns></returns>
        [ErrorFilter("添加失败", ErrorFilterType.ReturnValue)]
        public ActionResult DoAddUser()
        {
            yq.WebModel.Sys.AddUser addUser = new WebModel.Sys.AddUser();
            TryUpdateModel(addUser);
            var rv = yq.GetService.Create<yq.IService.IUser>().RegisterUser(addUser.Login, addUser.Account, addUser.Info);

            return Json(rv);
        }
    }
}
